import Vue from 'vue'
import moment from 'moment'
import store from '@/store'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

// 格式化图片地址
Vue.filter('dealImageUrl', function (url) {
  if (!url) return ''
  return (url.substr(0, 4) === 'http' || url.substr(0, 5) === 'https' || url.substr(0, 2) === '//') ? url : 'https://minio-cluster.primelifescience.com.cn' + url
})

// 常量转换
Vue.filter('enumValueByKey', function (value, enumObj, key = 'desc', nullKey) {
  if (value == null) {
    return nullKey || '-'
  }
  let target = ''
  Object.keys(enumObj).forEach(item => {
    const obj = enumObj[item]
    if (obj.value === parseInt(value)) {
      target = obj[key]
    }
  })
  return target
})
// 常量转换(值为中文的转换，用于日志列表)
Vue.filter('enumValueByKeyString', function (value, enumObj, key = 'desc') {
  if (value == null) {
    return ''
  }
  let target = ''
  Object.keys(enumObj).forEach(item => {
    const obj = enumObj[item]
    if (obj.value === (value)) {
      target = obj[key]
    }
  })
  return target
})
// 将手机号码脱敏处理
Vue.filter('formatPhone', function (val) {
  if (val) {
    return (val.toString().slice(0, 3) + '****' + val.toString().slice(val.length - 4))
  } else {
    return ''
  }
})
// 将身份证号码脱敏处理
Vue.filter('formatIdCard', function (val) {
  if (val) {
    return (val.toString().slice(0, 3) + '********' + val.toString().slice(val.length - 4))
  } else {
    return ''
  }
})
// 将姓名脱敏处理
Vue.filter('formatUserName', function (val) {
  if (val) {
    let newName = ''
    if (val.length === 2) {
      newName = val.slice(0, 1) + '*'
    } else if (val.length === 3) {
      newName = val.slice(0, 1) + '**'
    } else {
      newName = val.slice(0, 1) + '***'
    }
    return newName
  } else {
    return ''
  }
})
// 将邮箱脱敏处理
Vue.filter('formatEmail', function (email) {
  let newEmail = email
  if (String(email).indexOf('@') > 0) {
    const str = email.split('@')
    let s = ''
    if (str[0].length > 3) { // @前面多于3位
      for (let i = 3; i < str[0].length; i++) {
        s += '*'
      }
      newEmail = str[0].substr(0, 3) + s + '@' + str[1]
    } else { // @前面小于等于于3位
      for (let i = 1; i < str[0].length; i++) {
        s += '*'
      }
      newEmail = str[0].substr(0, 1) + s + '@' + str[1]
    }
  }
  return newEmail
})
// String参数是否为空
Vue.filter('isEmpty', function (param) {
  return param === null || param === '' || param === undefined
})

// 判断是否有权限
Vue.filter('isHasAuth', function (val) {
  const curRoles = store.getters.roles

  let flag = false
  if (curRoles && curRoles.length && curRoles.map(item => {
    return item.code
  }).indexOf(val) !== -1) {
  // 后端改为返回数组，不是对象数组
  // if (curRoles && curRoles.length && curRoles.map(item => { return item }).indexOf(val) !== -1) {
    flag = true
  }
  return flag
})

// 判断受试者是否可见
Vue.filter('isPtsVisible', function () {
  const curRoles = store.getters.roles
  let flag = false
  if (curRoles && curRoles.length) {
    if (curRoles[0].ptsVisible === 1) {
      flag = true
    }
  }
  return flag
})

// 银行卡号隐藏中间
Vue.filter('cardNoDeal', function (str) {
  if (!str) return
  const middleLength = str.length - 8
  return str.substr(0, 4) + Array(middleLength).fill('*').join('') + str.substr(-4)
})
// 处理金额
Vue.filter('formatAmount', function (val) {
  if (val) {
    return parseFloat(val.toFixed(2))
  } else {
    return val
  }
})
// 校验去除中文
Vue.filter('checkUnChinese', function (val) {
  val = val.replace(/[\u4E00-\u9FA5]/g, '')
  return val
})
// 获取页数
Vue.filter('getPageIndex', function (index, val, page) {
  index++
  if (val) {
    if (val.localPagination) {
      const localPagination = val.localPagination
      const start = localPagination.current ? localPagination.current : 1
      const end = localPagination.pageSize
      const mIndex = index + (start - 1) * end - 1
      const count = localPagination.total - mIndex
      return count > 0 ? count : 0
    } else if (page && page.pageSize) {
      return index + (page.pageNumber - 1) * page.pageSize
    } else {
      return index
    }
  } else {
    return index
  }
})

// 设置页数
Vue.filter('getIndexNumber', function (index, data) {
  if (data && data.length) {
    return data.length - index
  } else {
    return index
  }
})
// 校验金额为小数点后两位
Vue.filter('amountCheck', function (val) {
  val = val.replace('e', '')
  val = val.replace(/[^0-9.]/g, '')
  val = val.replace(/^()*(\d+)\.(\d\d).*$/, '$1$2.$3') // 此处还可以限制位数以及大小
  return val
})

// 处理空值，防止 undefined 展示在页面上
Vue.filter('formatNull', function (val, emptyString = '') {
  if (val === null || val === void 0) return emptyString
  return val
})
